<!DOCTYPE HTML>
<html lang="zh_CN">
<head><meta name="generator" content="Hexo 3.8.0">
    <!--Setting-->
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="IE=Edge,chrome=1">
    <meta http-equiv="Cache-Control" content="no-siteapp">
    <meta http-equiv="Cache-Control" content="no-transform">
    <meta name="renderer" content="webkit|ie-comp|ie-stand">
    <meta name="apple-mobile-web-app-capable" content="cn華少">
    <meta name="apple-mobile-web-app-status-bar-style" content="black">
    <meta name="format-detection" content="telephone=no,email=no,adress=no">
    <meta name="browsermode" content="application">
    <meta name="screen-orientation" content="portrait">
    <link rel="dns-prefetch" href="http://cnhuashao.com">
    <!--SEO-->

    <meta name="keywords" content="Dropwizard">


    <meta name="description" content="在开始之前我们需要准备一些环境，以下为本次所需相关工具：系统：windows 7JDK：1.8开发工具：IntelliJ IDEA 2017.2.5Maven：idea自带最新版本，采用本地化s...">



<meta name="robots" content="all">
<meta name="google" content="all">
<meta name="googlebot" content="all">
<meta name="verify" content="all">

    <!--Title-->


<title>使用Dropwizard搭建一个Hello World | cn華少</title>


    <link rel="alternate" href="/atom.xml" title="cn華少" type="application/atom+xml">


    <link rel="icon" href="/favicon/favicon.ico">

    



<link rel="stylesheet" href="/css/bootstrap.min.css?rev=3.3.7">
<link rel="stylesheet" href="/favicons.css">
<link rel="stylesheet" href="/css/style.css?rev=@@hash">




    
	<div class="hide">
		<script type="text/javascript">
			var cnzz_protocol = (("https:" == document.location.protocol) ? " https://" : " http://");document.write(unescape("%3Cspan class='cnzz_stat_icon_1263868967 hide' %3E%3Cscript%20src%3D%22https%3A%2F%2Fs95.cnzz.com%2Fz_stat.php%3Fweb_id%3D1272564536%22%3E%3C%2Fscript%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "s19.cnzz.com/z_stat.php%3Fid%3D1263868967%26show%3Dpic1' type='text/javascript'%3E%3C/script%3E"));
		</script>
	</div>






    

</head>

</html>
<!--[if lte IE 8]>
<style>
    html{ font-size: 1em }
</style>
<![endif]-->
<!--[if lte IE 9]>
<div style="ie">你使用的浏览器版本过低，为了你更好的阅读体验，请更新浏览器的版本或者使用其他现代浏览器，比如Chrome、Firefox、Safari等。</div>
<![endif]-->

<body>
    <header class="main-header" style="background-image:url(http://snippet.shenliyang.com/img/banner.jpg)">
    <div class="main-header-box">
        <a class="header-avatar" href="/" title="cn華少">
            <img src="/img/avatar.jpg" alt="logo头像" class="img-responsive center-block">
        </a>
        <div class="branding">
        	<!--<h2 class="text-hide">Snippet主题,从未如此简单有趣</h2>-->
            
                <h2> 追梦，无惧 </h2>
            
    	</div>
    </div>
</header>
    <nav class="main-navigation">
    <div class="container">
        <div class="row">
            <div class="col-sm-12">
                <div class="navbar-header"><span class="nav-toggle-button collapsed pull-right" data-toggle="collapse" data-target="#main-menu" id="mnav">
                    <span class="sr-only"></span>
                        <i class="fa fa-bars"></i>
                    </span>
                    <a class="navbar-brand" href="http://cnhuashao.com">cn華少</a>
                </div>
                <div class="collapse navbar-collapse" id="main-menu">
                    <ul class="menu">
                        
                            <li role="presentation" class="text-center">
                                <a href="/"><i class="fa "></i>首页</a>
                            </li>
                        
                            <li role="presentation" class="text-center">
                                <a href="/categories/java/"><i class="fa "></i>Java</a>
                            </li>
                        
                            <li role="presentation" class="text-center">
                                <a href="/categories/postgresql/"><i class="fa "></i>PostgreSQL</a>
                            </li>
                        
                            <li role="presentation" class="text-center">
                                <a href="/categories/工具/"><i class="fa "></i>工具</a>
                            </li>
                        
                            <li role="presentation" class="text-center">
                                <a href="/archives/"><i class="fa "></i>时间轴</a>
                            </li>
                        
                            <li role="presentation" class="text-center">
                                <a href="/about/"><i class="fa "></i>关于</a>
                            </li>
                        
                    </ul>
                </div>
            </div>
        </div>
    </div>
</nav>
    <section class="content-wrap">
        <div class="container">
            <div class="row">
                <main class="col-md-8 main-content m-post">
                    <p id="process"></p>
<article class="post">
    <div class="post-head">
        <h1 id="使用Dropwizard搭建一个Hello World">
            
	            使用Dropwizard搭建一个Hello World
            
        </h1>
        <div class="post-meta">
    
        <span class="categories-meta fa-wrap">
            <i class="fa fa-folder-open-o"></i>
            <a class="category-link" href="/categories/Dropwizard/">Dropwizard</a>
        </span>
    

    
        <span class="fa-wrap">
            <i class="fa fa-tags"></i>
            <span class="tags-meta">
                
                    <a class="tag-link" href="/tags/Dropwizard/">Dropwizard</a>
                
            </span>
        </span>
    

    
        
        <span class="fa-wrap">
            <i class="fa fa-clock-o"></i>
            <span class="date-meta">2019/04/18</span>
        </span>
        
    
</div>
            
            
    </div>
    
    <div class="post-body post-content">
        <h6 id="在开始之前我们需要准备一些环境，以下为本次所需相关工具："><a href="#在开始之前我们需要准备一些环境，以下为本次所需相关工具：" class="headerlink" title="在开始之前我们需要准备一些环境，以下为本次所需相关工具："></a>在开始之前我们需要准备一些环境，以下为本次所需相关工具：</h6><h6 id="系统：windows-7"><a href="#系统：windows-7" class="headerlink" title="系统：windows 7"></a>系统：windows 7</h6><h6 id="JDK：1-8"><a href="#JDK：1-8" class="headerlink" title="JDK：1.8"></a>JDK：1.8</h6><h6 id="开发工具：IntelliJ-IDEA-2017-2-5"><a href="#开发工具：IntelliJ-IDEA-2017-2-5" class="headerlink" title="开发工具：IntelliJ IDEA 2017.2.5"></a>开发工具：IntelliJ IDEA 2017.2.5</h6><h6 id="Maven：idea自带最新版本，采用本地化setting-xml（可以参考阿里巴巴的源配置，这样创建项目时自动下载要快很多倍）"><a href="#Maven：idea自带最新版本，采用本地化setting-xml（可以参考阿里巴巴的源配置，这样创建项目时自动下载要快很多倍）" class="headerlink" title="Maven：idea自带最新版本，采用本地化setting.xml（可以参考阿里巴巴的源配置，这样创建项目时自动下载要快很多倍）"></a>Maven：idea自带最新版本，采用本地化setting.xml（可以参考阿里巴巴的源配置，这样创建项目时自动下载要快很多倍）</h6><p>本次编写demo参考依据为<a href="http://dropwizard.readthedocs.io" target="_blank" rel="noopener">Dropwizard官网</a><br><a id="more"></a></p>
<h4 id="下面我们开始进入正题，创建第一个基于Dropwizard的Hello-World"><a href="#下面我们开始进入正题，创建第一个基于Dropwizard的Hello-World" class="headerlink" title="下面我们开始进入正题，创建第一个基于Dropwizard的Hello World"></a>下面我们开始进入正题，创建第一个基于Dropwizard的Hello World</h4><h3 id="一、创建一个maven的web项目"><a href="#一、创建一个maven的web项目" class="headerlink" title="一、创建一个maven的web项目"></a>一、创建一个maven的web项目</h3><p><img src="http://upload-images.jianshu.io/upload_images/5330898-33b5ccfeaf919a6f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="1.png"></p>
<p><img src="http://upload-images.jianshu.io/upload_images/5330898-a061b04464da528e.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="2.png"></p>
<p><img src="http://upload-images.jianshu.io/upload_images/5330898-6979aa8bf1c7a346.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="3.png"></p>
<p><img src="http://upload-images.jianshu.io/upload_images/5330898-ad1694dbddb24435.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="4.png"></p>
<h6 id="这里在创建时因为我本地已经存在了settings-xml，因此选择本地化配置文件，如果采用默认的则不需要进行选择。"><a href="#这里在创建时因为我本地已经存在了settings-xml，因此选择本地化配置文件，如果采用默认的则不需要进行选择。" class="headerlink" title="这里在创建时因为我本地已经存在了settings.xml，因此选择本地化配置文件，如果采用默认的则不需要进行选择。"></a>这里在创建时因为我本地已经存在了settings.xml，因此选择本地化配置文件，如果采用默认的则不需要进行选择。</h6><p><img src="http://upload-images.jianshu.io/upload_images/5330898-58b2b085a887fba9.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="5.png"></p>
<p><img src="http://upload-images.jianshu.io/upload_images/5330898-66cc5a237ce89463.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="6.png"></p>
<h6 id="点击图中标记的位置，将相关包进行下载并加入引用中，其实默认就一个junit包引用而已。"><a href="#点击图中标记的位置，将相关包进行下载并加入引用中，其实默认就一个junit包引用而已。" class="headerlink" title="点击图中标记的位置，将相关包进行下载并加入引用中，其实默认就一个junit包引用而已。"></a>点击图中标记的位置，将相关包进行下载并加入引用中，其实默认就一个junit包引用而已。</h6><h3 id="二、开始进行创建基础包路径"><a href="#二、开始进行创建基础包路径" class="headerlink" title="二、开始进行创建基础包路径"></a>二、开始进行创建基础包路径</h3><p><img src="http://upload-images.jianshu.io/upload_images/5330898-e5e44a56d4788759.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p><img src="http://upload-images.jianshu.io/upload_images/5330898-eb9b8ef14fe37455.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<h6 id="创建一个java目录，并设置该目录为根目录。"><a href="#创建一个java目录，并设置该目录为根目录。" class="headerlink" title="创建一个java目录，并设置该目录为根目录。"></a>创建一个java目录，并设置该目录为根目录。</h6><h3 id="三、在pom-xml中引入dependency基础包。"><a href="#三、在pom-xml中引入dependency基础包。" class="headerlink" title="三、在pom.xml中引入dependency基础包。"></a>三、在pom.xml中引入dependency基础包。</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">&lt;properties&gt;</span><br><span class="line">    &lt;dependency.version&gt;1.2.0&lt;/dependency.version&gt;</span><br><span class="line">  &lt;/properties&gt;</span><br><span class="line">  &lt;dependencies&gt;</span><br><span class="line">    &lt;!-- Dropwizard基础包 --&gt;</span><br><span class="line">    &lt;dependency&gt;</span><br><span class="line">      &lt;groupId&gt;io.dropwizard&lt;/groupId&gt;</span><br><span class="line">      &lt;artifactId&gt;dropwizard-core&lt;/artifactId&gt;</span><br><span class="line">      &lt;version&gt;$&#123;dependency.version&#125;&lt;/version&gt;</span><br><span class="line">    &lt;/dependency&gt;</span><br><span class="line"></span><br><span class="line">  &lt;/dependencies&gt;</span><br></pre></td></tr></table></figure>
<h3 id="四、创建基础yml配置文件"><a href="#四、创建基础yml配置文件" class="headerlink" title="四、创建基础yml配置文件"></a>四、创建基础yml配置文件</h3><p><img src="http://upload-images.jianshu.io/upload_images/5330898-0cb1e90176ee03bd.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"><br>该demo1.yml文件中的内容为：<br><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">#要打印的基础语句，其中%s是占位符。</span><br><span class="line">template: Hello, %s!</span><br><span class="line">#默认引用时的名字，这里名字叫做Stranger</span><br><span class="line">defaultName: Stranger</span><br></pre></td></tr></table></figure></p>
<h6 id="在这里开始进入Dropwizard特性的一些编写了。"><a href="#在这里开始进入Dropwizard特性的一些编写了。" class="headerlink" title="在这里开始进入Dropwizard特性的一些编写了。"></a>在这里开始进入Dropwizard特性的一些编写了。</h6><h6 id="yml配置文件在官方解释中是这样说的："><a href="#yml配置文件在官方解释中是这样说的：" class="headerlink" title="yml配置文件在官方解释中是这样说的："></a>yml配置文件在官方解释中是这样说的：</h6><blockquote>
<p>Each Dropwizard application has its own subclass of the Configuration<br> class which specifies environment-specific parameters. These parameters are specified in a <a href="http://www.yaml.org/" target="_blank" rel="noopener">YAML</a> configuration file which is deserialized to an instance of your application’s configuration class and validated.<br>The application we’ll be building is a high-performance Hello World service, and one of our requirements is that we need to be able to vary how it says hello from environment to environment. We’ll need to specify at least two things to begin with: a template for saying hello and a default name to use in case the user doesn’t specify their name.</p>
</blockquote>
<h6 id="因此我这里在写hello-world时就直接按照上面说的来了。word在模板里面先用占位符，这样就可以-hello，小明-了o-╯□╰-o"><a href="#因此我这里在写hello-world时就直接按照上面说的来了。word在模板里面先用占位符，这样就可以-hello，小明-了o-╯□╰-o" class="headerlink" title="因此我这里在写hello world时就直接按照上面说的来了。word在模板里面先用占位符，这样就可以[hello，小明]了o(╯□╰)o"></a>因此我这里在写hello world时就直接按照上面说的来了。word在模板里面先用占位符，这样就可以[hello，小明]了o(╯□╰)o</h6><h3 id="五、创建基础Configuration，用于进行反序列化。"><a href="#五、创建基础Configuration，用于进行反序列化。" class="headerlink" title="五、创建基础Configuration，用于进行反序列化。"></a>五、创建基础Configuration，用于进行反序列化。</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br></pre></td><td class="code"><pre><span class="line">package com.demo1.helloworld.configuration;</span><br><span class="line"></span><br><span class="line">import com.fasterxml.jackson.annotation.JsonProperty;</span><br><span class="line">import io.dropwizard.Configuration;</span><br><span class="line">import org.hibernate.validator.constraints.NotEmpty;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * &lt;b&gt;TODO(com.demo1.helloworld.configuration @TODO)&lt;/b&gt;&lt;br&gt;</span><br><span class="line"> * 版权所有 &lt;a href=&quot;http://www.whndj.com&quot;&gt;中国，華少&lt;/a&gt;&lt;br&gt;</span><br><span class="line"> * 本软件仅对本次教程负责，其版权归属cnHuaShao，如需引用，请申明其版权所有人。</span><br><span class="line"> *</span><br><span class="line"> * @Description: TODO 创建人：toplion</span><br><span class="line"> * @CreateDate 2017 2017/11/4 10:31 11 修改人：toplion</span><br><span class="line"> * &lt;a href=&quot;mailto:lz2392504@gmail.com&quot;&gt;cnHuaShao&lt;/a&gt;</span><br><span class="line"> * 修改备注：</span><br><span class="line"> * @since V1.0</span><br><span class="line"> */</span><br><span class="line">public class HelloWorldConfiguration extends Configuration &#123;</span><br><span class="line"></span><br><span class="line">    /**</span><br><span class="line">     * NotEmpty注解表示该变量不可以为空，之前我们在demo1.yml中已经编写了该变量对应的值，这个类主要是将其进行反序列化</span><br><span class="line">     */</span><br><span class="line">    @NotEmpty</span><br><span class="line">    private String template;</span><br><span class="line"></span><br><span class="line">    /**</span><br><span class="line">     * NotEmpty注解表示该变量不可以为空，之前我们在demo1.yml中已经编写了该变量对应的值，这个类主要是将其进行反序列化</span><br><span class="line">     * 默认值直接写入Stranger，这里根据自己的demo1.yml中defaultName默认值写的是什么就直接赋值什么，当然也可以不赋值，等具体引用时在赋值，不过建议还是写上去一个默认值，防止它出错。</span><br><span class="line">     * 如果这两个值是空，则抛出系统异常</span><br><span class="line">     */</span><br><span class="line">    @NotEmpty</span><br><span class="line">    private String defaultName = &quot;Stranger&quot;;</span><br><span class="line"></span><br><span class="line">    /**</span><br><span class="line">     * 这里get set进行注解JsonProperty主要是为了对yml进行反序列化使用</span><br><span class="line">     * @return</span><br><span class="line">     */</span><br><span class="line">    @JsonProperty</span><br><span class="line">    public String getTemplate() &#123;</span><br><span class="line">        return template;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @JsonProperty</span><br><span class="line">    public void setTemplate(String template) &#123;</span><br><span class="line">        this.template = template;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @JsonProperty</span><br><span class="line">    public String getDefaultName() &#123;</span><br><span class="line">        return defaultName;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @JsonProperty</span><br><span class="line">    public void setDefaultName(String name) &#123;</span><br><span class="line">        this.defaultName = name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>官方给出的解释是：</p>
<blockquote>
<p>Each Dropwizard application has its own subclass of the Configuration<br> class which specifies environment-specific parameters. These parameters are specified in a <a href="http://www.yaml.org/" target="_blank" rel="noopener">YAML</a> configuration file which is deserialized to an instance of your application’s configuration class and validated.<br>The application we’ll be building is a high-performance Hello World service, and one of our requirements is that we need to be able to vary how it says hello from environment to environment. We’ll need to specify at least two things to begin with: a template for saying hello and a default name to use in case the user doesn’t specify their name.</p>
</blockquote>
<h3 id="六、进行创建基础Application"><a href="#六、进行创建基础Application" class="headerlink" title="六、进行创建基础Application"></a>六、进行创建基础Application</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><span class="line">package com.demo1.helloworld.application;</span><br><span class="line"></span><br><span class="line">import com.demo1.helloworld.configuration.HelloWorldConfiguration;</span><br><span class="line">import io.dropwizard.Application;</span><br><span class="line">import io.dropwizard.setup.Environment;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * &lt;b&gt;TODO(com.demo1.helloworld.application @TODO)&lt;/b&gt;&lt;br&gt;</span><br><span class="line"> * 版权所有 &lt;a href=&quot;http://www.whndj.com&quot;&gt;中国，華少&lt;/a&gt;&lt;br&gt;</span><br><span class="line"> * 本软件仅对本次教程负责，其版权归属cnHuaShao，如需引用，请申明其版权所有人。</span><br><span class="line"> *</span><br><span class="line"> * @Description: TODO 创建人：toplion</span><br><span class="line"> * @CreateDate 2017 2017/11/4 10:58 11 修改人：toplion</span><br><span class="line"> * &lt;a href=&quot;mailto:lz2392504@gmail.com&quot;&gt;cnHuaShao&lt;/a&gt;</span><br><span class="line"> * 修改备注：</span><br><span class="line"> * @since V1.0</span><br><span class="line"> */</span><br><span class="line">public class HelloWorldApplication  extends Application&lt;HelloWorldConfiguration&gt; &#123;</span><br><span class="line"></span><br><span class="line">    /**</span><br><span class="line">     * 基础核心启动方法。dropwizard应用的核心方法。主要进行处理具体的业务逻辑的类</span><br><span class="line">     * @param configuration</span><br><span class="line">     * @param environment</span><br><span class="line">     * @throws Exception</span><br><span class="line">     */</span><br><span class="line">    public void run(HelloWorldConfiguration configuration, Environment environment) throws Exception &#123;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Override</span><br><span class="line">    public String getName() &#123;</span><br><span class="line">        return &quot;hello-world&quot;;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="七、创建一个基础表示类，其需要使其转成JSON时遵循RFC-1149"><a href="#七、创建一个基础表示类，其需要使其转成JSON时遵循RFC-1149" class="headerlink" title="七、创建一个基础表示类，其需要使其转成JSON时遵循RFC 1149"></a>七、创建一个基础表示类，其需要使其转成JSON时遵循<a href="http://www.ietf.org/rfc/rfc1149.txt" target="_blank" rel="noopener">RFC 1149</a></h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><span class="line">package com.demo1.helloworld.api;</span><br><span class="line"></span><br><span class="line">import com.fasterxml.jackson.annotation.JsonProperty;</span><br><span class="line">import org.hibernate.validator.constraints.Length;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * &lt;b&gt;TODO(com.demo1.helloworld.api @TODO)&lt;/b&gt;&lt;br&gt;</span><br><span class="line"> * 版权所有 &lt;a href=&quot;http://www.whndj.com&quot;&gt;中国，華少&lt;/a&gt;&lt;br&gt;</span><br><span class="line"> * 本软件仅对本次教程负责，其版权归属cnHuaShao，如需引用，请申明其版权所有人。</span><br><span class="line"> *</span><br><span class="line"> * @Description: TODO 创建人：toplion</span><br><span class="line"> * @CreateDate 2017 2017/11/4 11:07 11 修改人：toplion</span><br><span class="line"> * &lt;a href=&quot;mailto:lz2392504@gmail.com&quot;&gt;cnHuaShao&lt;/a&gt;</span><br><span class="line"> * 修改备注：</span><br><span class="line"> * @since V1.0</span><br><span class="line"> */</span><br><span class="line">public class Saying &#123;</span><br><span class="line"></span><br><span class="line">    private long id;</span><br><span class="line"></span><br><span class="line">    /**</span><br><span class="line">     * 内容最大长度不可以超过10</span><br><span class="line">     */</span><br><span class="line">    @Length(max = 10)</span><br><span class="line">    private String content;</span><br><span class="line"></span><br><span class="line">    public Saying() &#123;</span><br><span class="line">        // Jackson deserialization</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    public Saying(long id, String content) &#123;</span><br><span class="line">        this.id = id;</span><br><span class="line">        this.content = content;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @JsonProperty</span><br><span class="line">    public long getId() &#123;</span><br><span class="line">        return id;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @JsonProperty</span><br><span class="line">    public String getContent() &#123;</span><br><span class="line">        return content;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="八、创建Resource-类"><a href="#八、创建Resource-类" class="headerlink" title="八、创建Resource 类"></a>八、创建Resource 类</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line">package com.demo1.helloworld.resources;</span><br><span class="line"></span><br><span class="line">import com.codahale.metrics.annotation.Timed;</span><br><span class="line">import com.demo1.helloworld.api.Saying;</span><br><span class="line"></span><br><span class="line">import javax.ws.rs.GET;</span><br><span class="line">import javax.ws.rs.Path;</span><br><span class="line">import javax.ws.rs.Produces;</span><br><span class="line">import javax.ws.rs.QueryParam;</span><br><span class="line">import javax.ws.rs.core.MediaType;</span><br><span class="line">import java.util.concurrent.atomic.AtomicLong;</span><br><span class="line">import java.util.Optional;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * &lt;b&gt;TODO(com.demo1.helloworld.resources @TODO)&lt;/b&gt;&lt;br&gt;</span><br><span class="line"> * 版权所有 &lt;a href=&quot;http://www.whndj.com&quot;&gt;中国，華少&lt;/a&gt;&lt;br&gt;</span><br><span class="line"> * 本软件仅对本次教程负责，其版权归属cnHuaShao，如需引用，请申明其版权所有人。</span><br><span class="line"> *</span><br><span class="line"> * @Description: TODO 创建人：toplion</span><br><span class="line"> * @CreateDate 2017 2017/11/4 11:19 11 修改人：toplion</span><br><span class="line"> * &lt;a href=&quot;mailto:lz2392504@gmail.com&quot;&gt;cnHuaShao&lt;/a&gt;</span><br><span class="line"> * 修改备注：</span><br><span class="line"> * @since V1.0</span><br><span class="line"> */</span><br><span class="line">@Path(&quot;/hello-world&quot;)//跳转地址</span><br><span class="line">@Produces(MediaType.APPLICATION_JSON)//返回的数据类型</span><br><span class="line">public class HelloWorldResource &#123;</span><br><span class="line"></span><br><span class="line">    private final String template;</span><br><span class="line">    private final String defaultName;</span><br><span class="line">    private final AtomicLong counter;</span><br><span class="line"></span><br><span class="line">    public HelloWorldResource(String template, String defaultName) &#123;</span><br><span class="line">        this.template = template;</span><br><span class="line">        this.defaultName = defaultName;</span><br><span class="line">        this.counter = new AtomicLong();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    /**</span><br><span class="line">     * @Timed dropwizard会自动记录时间和它的调用率作为度量定时器</span><br><span class="line">     * @param name</span><br><span class="line">     * @return</span><br><span class="line">     * @QueryParam 为URL链接后面的参数（url ？&#123;参数名&#125; = &#123;参数值&#125; 的格式）</span><br><span class="line">     */</span><br><span class="line">    @GET//客户端访问时只能通过get方法访问</span><br><span class="line">    @Timed</span><br><span class="line">    public Saying sayHello(@QueryParam(&quot;name&quot;) Optional&lt;String&gt; name) &#123;</span><br><span class="line">        //如果name参数没有传入，则调用defaultName的值，如果传入了，则采用name的值</span><br><span class="line">        final String value = String.format(template, name.orElse(defaultName));</span><br><span class="line">        //返回Saying对象</span><br><span class="line">        return new Saying(counter.incrementAndGet(), value);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<h3 id="九、修改第六步时创建的Application中的run方法。"><a href="#九、修改第六步时创建的Application中的run方法。" class="headerlink" title="九、修改第六步时创建的Application中的run方法。"></a>九、修改第六步时创建的Application中的run方法。</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><span class="line">public void run(HelloWorldConfiguration configuration, Environment environment) throws Exception &#123;</span><br><span class="line">        //声明一个resource，并传入初始化的值，该值由configuration读取的yml配置文件中的值。</span><br><span class="line">        //可以启动多个resource</span><br><span class="line">        final HelloWorldResource resource = new HelloWorldResource(</span><br><span class="line">                configuration.getTemplate(),</span><br><span class="line">                configuration.getDefaultName()</span><br><span class="line">        );</span><br><span class="line"></span><br><span class="line">        environment.jersey().register(resource);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<h3 id="十、创建一个HealthCheck类"><a href="#十、创建一个HealthCheck类" class="headerlink" title="十、创建一个HealthCheck类"></a>十、创建一个HealthCheck类</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br></pre></td><td class="code"><pre><span class="line">package com.demo1.helloworld.health;</span><br><span class="line"></span><br><span class="line">import com.codahale.metrics.health.HealthCheck;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * &lt;b&gt;TODO(com.demo1.helloworld.health @TODO)&lt;/b&gt;&lt;br&gt;</span><br><span class="line"> * 版权所有 &lt;a href=&quot;http://www.whndj.com&quot;&gt;中国，華少&lt;/a&gt;&lt;br&gt;</span><br><span class="line"> * 本软件仅对本次教程负责，其版权归属cnHuaShao，如需引用，请申明其版权所有人。</span><br><span class="line"> *</span><br><span class="line"> * @Description: TODO 创建人：toplion</span><br><span class="line"> * @CreateDate 2017 2017/11/4 11:37 11 修改人：toplion</span><br><span class="line"> * &lt;a href=&quot;mailto:lz2392504@gmail.com&quot;&gt;cnHuaShao&lt;/a&gt;</span><br><span class="line"> * 修改备注：</span><br><span class="line"> * @since V1.0</span><br><span class="line"> */</span><br><span class="line">public class TemplateHealthCheck extends HealthCheck &#123;</span><br><span class="line">    private final String template;</span><br><span class="line"></span><br><span class="line">    public TemplateHealthCheck(String template) &#123;</span><br><span class="line">        this.template = template;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    @Override</span><br><span class="line">    protected Result check() throws Exception &#123;</span><br><span class="line">        final String saying = String.format(template, &quot;模板测试&quot;);</span><br><span class="line">        if (!saying.contains(&quot;模板测试&quot;)) &#123;</span><br><span class="line">            return Result.unhealthy(&quot;该模板不包含名称&quot;);</span><br><span class="line">        &#125;</span><br><span class="line">        return Result.healthy();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>官方给出的解释是这样的：</p>
<blockquote>
<p>Health checks give you a way of adding small tests to your application to allow you to verify that your application is functioning correctly in production. We strongly recommend that all of your applications have at least a minimal set of health checks.<br>其实这个是可有可无的，为了安全起见，这里先加上。（官方例子中要求加上o(╯□╰)o）</p>
</blockquote>
<h3 id="十一、再次修改第六步时创建的Application中的run方法。"><a href="#十一、再次修改第六步时创建的Application中的run方法。" class="headerlink" title="十一、再次修改第六步时创建的Application中的run方法。"></a>十一、再次修改第六步时创建的Application中的run方法。</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><span class="line">public void run(HelloWorldConfiguration configuration, Environment environment) throws Exception &#123;</span><br><span class="line">        //声明一个resource，并传入初始化的值，该值由configuration读取的yml配置文件中的值。</span><br><span class="line">        //可以启动多个resource</span><br><span class="line">        final HelloWorldResource resource = new HelloWorldResource(</span><br><span class="line">                configuration.getTemplate(),</span><br><span class="line">                configuration.getDefaultName()</span><br><span class="line">        );</span><br><span class="line"></span><br><span class="line">        //声明一个检查点，让其进行检查该模板的正确性。</span><br><span class="line">        final TemplateHealthCheck healthCheck =</span><br><span class="line">                new TemplateHealthCheck(configuration.getTemplate());</span><br><span class="line">        environment.healthChecks().register(&quot;template&quot;, healthCheck);</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">        environment.jersey().register(resource);</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<h3 id="十一、创建一个main方法。"><a href="#十一、创建一个main方法。" class="headerlink" title="十一、创建一个main方法。"></a>十一、创建一个main方法。</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">package com.demo1.helloworld.main;</span><br><span class="line"></span><br><span class="line">import com.demo1.helloworld.application.HelloWorldApplication;</span><br><span class="line"></span><br><span class="line">/**</span><br><span class="line"> * &lt;b&gt;TODO(com.demo1.helloworld.main @TODO)&lt;/b&gt;&lt;br&gt;</span><br><span class="line"> * 版权所有 &lt;a href=&quot;http://www.whndj.com&quot;&gt;中国，華少&lt;/a&gt;&lt;br&gt;</span><br><span class="line"> * 本软件仅对本次教程负责，其版权归属cnHuaShao，如需引用，请申明其版权所有人。</span><br><span class="line"> *</span><br><span class="line"> * @Description: TODO 创建人：toplion</span><br><span class="line"> * @CreateDate 2017 2017/11/4 11:00 11 修改人：toplion</span><br><span class="line"> * &lt;a href=&quot;mailto:lz2392504@gmail.com&quot;&gt;cnHuaShao&lt;/a&gt;</span><br><span class="line"> * 修改备注：</span><br><span class="line"> * @since V1.0</span><br><span class="line"> */</span><br><span class="line">public class HelloWorldMain &#123;</span><br><span class="line"></span><br><span class="line">    public static void main(String[] args) throws Exception &#123;</span><br><span class="line">        //启动helloworld</span><br><span class="line">        new HelloWorldApplication().run(args);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>官方例子中main方法写在Application类中，我这里将其提出来了，方便大家理解。</p>
<p>#至此，我们这个例子基本上写完了，开始进行打包。</p>
<h3 id="十二、修改maven的pom-xml文件"><a href="#十二、修改maven的pom-xml文件" class="headerlink" title="十二、修改maven的pom.xml文件"></a>十二、修改maven的pom.xml文件</h3><figure class="highlight plain"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br></pre></td><td class="code"><pre><span class="line">&lt;plugins&gt;</span><br><span class="line">          &lt;plugin&gt;</span><br><span class="line">              &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;</span><br><span class="line">              &lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;</span><br><span class="line">              &lt;configuration&gt;</span><br><span class="line">                  &lt;source&gt;1.8&lt;/source&gt;</span><br><span class="line">                  &lt;target&gt;1.8&lt;/target&gt;</span><br><span class="line">              &lt;/configuration&gt;</span><br><span class="line">          &lt;/plugin&gt;</span><br><span class="line">          &lt;plugin&gt;</span><br><span class="line">              &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;</span><br><span class="line">              &lt;artifactId&gt;maven-shade-plugin&lt;/artifactId&gt;</span><br><span class="line">              &lt;version&gt;2.3&lt;/version&gt;</span><br><span class="line">              &lt;configuration&gt;</span><br><span class="line">                  &lt;createDependencyReducedPom&gt;true&lt;/createDependencyReducedPom&gt;</span><br><span class="line">                  &lt;filters&gt;</span><br><span class="line">                      &lt;filter&gt;</span><br><span class="line">                          &lt;artifact&gt;*:*&lt;/artifact&gt;</span><br><span class="line">                          &lt;excludes&gt;</span><br><span class="line">                              &lt;exclude&gt;META-INF/*.SF&lt;/exclude&gt;</span><br><span class="line">                              &lt;exclude&gt;META-INF/*.DSA&lt;/exclude&gt;</span><br><span class="line">                              &lt;exclude&gt;META-INF/*.RSA&lt;/exclude&gt;</span><br><span class="line">                          &lt;/excludes&gt;</span><br><span class="line">                      &lt;/filter&gt;</span><br><span class="line">                  &lt;/filters&gt;</span><br><span class="line">              &lt;/configuration&gt;</span><br><span class="line">              &lt;executions&gt;</span><br><span class="line">                  &lt;execution&gt;</span><br><span class="line">                      &lt;phase&gt;package&lt;/phase&gt;</span><br><span class="line">                      &lt;goals&gt;</span><br><span class="line">                          &lt;goal&gt;shade&lt;/goal&gt;</span><br><span class="line">                      &lt;/goals&gt;</span><br><span class="line">                      &lt;configuration&gt;</span><br><span class="line">                          &lt;transformers&gt;</span><br><span class="line">                              &lt;transformer implementation=&quot;org.apache.maven.plugins.shade.resource.ServicesResourceTransformer&quot;/&gt;</span><br><span class="line">                              &lt;transformer implementation=&quot;org.apache.maven.plugins.shade.resource.ManifestResourceTransformer&quot;&gt;</span><br><span class="line">                                  &lt;mainClass&gt;com.demo1.helloworld.main.HelloWorldMain&lt;/mainClass&gt;</span><br><span class="line">                              &lt;/transformer&gt;</span><br><span class="line">                          &lt;/transformers&gt;</span><br><span class="line">                      &lt;/configuration&gt;</span><br><span class="line">                  &lt;/execution&gt;</span><br><span class="line">              &lt;/executions&gt;</span><br><span class="line">          &lt;/plugin&gt;</span><br><span class="line">          &lt;plugin&gt;</span><br><span class="line">              &lt;groupId&gt;org.apache.maven.plugins&lt;/groupId&gt;</span><br><span class="line">              &lt;artifactId&gt;maven-jar-plugin&lt;/artifactId&gt;</span><br><span class="line">              &lt;version&gt;2.4&lt;/version&gt;</span><br><span class="line">              &lt;configuration&gt;</span><br><span class="line">                  &lt;archive&gt;</span><br><span class="line">                      &lt;manifest&gt;</span><br><span class="line">                          &lt;addDefaultImplementationEntries&gt;true&lt;/addDefaultImplementationEntries&gt;</span><br><span class="line">                      &lt;/manifest&gt;</span><br><span class="line">                  &lt;/archive&gt;</span><br><span class="line">              &lt;/configuration&gt;</span><br><span class="line">          &lt;/plugin&gt;</span><br><span class="line">      &lt;/plugins&gt;</span><br></pre></td></tr></table></figure>
<h3 id="十三、开始进行打包"><a href="#十三、开始进行打包" class="headerlink" title="十三、开始进行打包"></a>十三、开始进行打包</h3><p><img src="http://upload-images.jianshu.io/upload_images/5330898-23573f6797dd41d0.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p><img src="http://upload-images.jianshu.io/upload_images/5330898-cd8de4368f0caa97.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p><img src="http://upload-images.jianshu.io/upload_images/5330898-a4d400f7b641721d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p><img src="http://upload-images.jianshu.io/upload_images/5330898-4cb7ee341a1311e4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p><img src="http://upload-images.jianshu.io/upload_images/5330898-7bcb99846c97e8f4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p><img src="http://upload-images.jianshu.io/upload_images/5330898-8008602dfc79946f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p><img src="http://upload-images.jianshu.io/upload_images/5330898-ba9b1fe735ccee4f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<h3 id="十四、开始启动"><a href="#十四、开始启动" class="headerlink" title="十四、开始启动"></a>十四、开始启动</h3><p><img src="http://upload-images.jianshu.io/upload_images/5330898-bc811116b6479d6a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p><img src="http://upload-images.jianshu.io/upload_images/5330898-f1364cb228994e0a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p><img src="http://upload-images.jianshu.io/upload_images/5330898-be2ea350d21e9620.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p><img src="http://upload-images.jianshu.io/upload_images/5330898-479c0a339385a345.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<h4 id="看到上面的日志，则表明启动成功，打开浏览器访问："><a href="#看到上面的日志，则表明启动成功，打开浏览器访问：" class="headerlink" title="看到上面的日志，则表明启动成功，打开浏览器访问："></a>看到上面的日志，则表明启动成功，打开浏览器访问：</h4><p><a href="http://localhost:8080/hello-world" target="_blank" rel="noopener">http://localhost:8080/hello-world</a></p>
<p><img src="http://upload-images.jianshu.io/upload_images/5330898-293e1f3f01e192ca.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<p><a href="http://localhost:8080/hello-world?name=xiaoming" target="_blank" rel="noopener">http://localhost:8080/hello-world?name=xiaoming</a></p>
<p><img src="http://upload-images.jianshu.io/upload_images/5330898-f2b83d3033d764bb.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240" alt="image.png"></p>
<h4 id="至此，本次的Hello-World编写完成。"><a href="#至此，本次的Hello-World编写完成。" class="headerlink" title="至此，本次的Hello World编写完成。"></a>至此，本次的Hello World编写完成。</h4><p>所有代码均可以通过我的GItHub进行获取到，如有需要可以点击我的GitHub下载。</p>

    </div>
    
        <div class="reward" ontouchstart>
    <div class="reward-wrap">赏
        <div class="reward-box">
            
                <span class="reward-type">
                    <img class="alipay" src="/img/alipay.jpg"><b>支付宝打赏</b>
                </span>
            
            
                <span class="reward-type">
                    <img class="wechat" src="/img/wepay.jpg"><b>微信打赏</b>
                </span>
            
        </div>
    </div>
    <p class="reward-tip">赞赏是不耍流氓的鼓励</p>
</div>


    
    <div class="post-footer">
        <div>
            
                转载声明：商业转载请联系作者获得授权,非商业转载请注明出处 © <a href target="_blank">Snippet</a>
            
        </div>
        <div>
            
        </div>
    </div>
</article>

<div class="article-nav prev-next-wrap clearfix">
    
        <a href="/2019/04/18/owncloud-sslpeizhi/" class="pre-post btn btn-default" title="使用namecheap证书进行配置htmls安全访问">
            <i class="fa fa-angle-left fa-fw"></i><span class="hidden-lg">上一篇</span>
            <span class="hidden-xs">使用namecheap证书进行配置htmls安全访问</span>
        </a>
    
    
        <a href="/2019/04/18/jetty-init/" class="next-post btn btn-default" title="在Ubuntu18.04 Server中安装jetty，非apt版">
            <span class="hidden-lg">下一篇</span>
            <span class="hidden-xs">在Ubuntu18.04 Server中安装jetty，非apt版</span><i class="fa fa-angle-right fa-fw"></i>
        </a>
    
</div>


    <div id="comments">
        
	
    <div id="vcomments" class="valine"></div>
    <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
<script src="/assets/valine.min.js"></script>

    <script>
        new Valine({
            av: AV,
            el: '#vcomments',
            appId: 'xOKV9J4UeQAtVkvnJC7Kq2Jn-gzGzoHsz',
            appKey: 'erIpQac4azoCmgfBB7Dl9maa',
            placeholder: '说点什么吧',
            notify: false,
            verify: false,
            avatar: 'mm',
            meta: 'nick,mail'.split(','),
            pageSize: '10',
            path: window.location.pathname,
            lang: 'zh_CN'.toLowerCase()
        })
    </script>


    </div>





                </main>
                
                    <aside id="article-toc" role="navigation" class="col-md-4">
    <div class="widget">
        <h3 class="title">Table of Contents</h3>
        
            <ol class="toc"><li class="toc-item toc-level-6"><a class="toc-link" href="#在开始之前我们需要准备一些环境，以下为本次所需相关工具："><span class="toc-text">在开始之前我们需要准备一些环境，以下为本次所需相关工具：</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#系统：windows-7"><span class="toc-text">系统：windows 7</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#JDK：1-8"><span class="toc-text">JDK：1.8</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#开发工具：IntelliJ-IDEA-2017-2-5"><span class="toc-text">开发工具：IntelliJ IDEA 2017.2.5</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#Maven：idea自带最新版本，采用本地化setting-xml（可以参考阿里巴巴的源配置，这样创建项目时自动下载要快很多倍）"><span class="toc-text">Maven：idea自带最新版本，采用本地化setting.xml（可以参考阿里巴巴的源配置，这样创建项目时自动下载要快很多倍）</span></a></li></ol><li class="toc-item toc-level-4"><a class="toc-link" href="#下面我们开始进入正题，创建第一个基于Dropwizard的Hello-World"><span class="toc-text">下面我们开始进入正题，创建第一个基于Dropwizard的Hello World</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#一、创建一个maven的web项目"><span class="toc-text">一、创建一个maven的web项目</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#这里在创建时因为我本地已经存在了settings-xml，因此选择本地化配置文件，如果采用默认的则不需要进行选择。"><span class="toc-text">这里在创建时因为我本地已经存在了settings.xml，因此选择本地化配置文件，如果采用默认的则不需要进行选择。</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#点击图中标记的位置，将相关包进行下载并加入引用中，其实默认就一个junit包引用而已。"><span class="toc-text">点击图中标记的位置，将相关包进行下载并加入引用中，其实默认就一个junit包引用而已。</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#二、开始进行创建基础包路径"><span class="toc-text">二、开始进行创建基础包路径</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#创建一个java目录，并设置该目录为根目录。"><span class="toc-text">创建一个java目录，并设置该目录为根目录。</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#三、在pom-xml中引入dependency基础包。"><span class="toc-text">三、在pom.xml中引入dependency基础包。</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#四、创建基础yml配置文件"><span class="toc-text">四、创建基础yml配置文件</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#在这里开始进入Dropwizard特性的一些编写了。"><span class="toc-text">在这里开始进入Dropwizard特性的一些编写了。</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#yml配置文件在官方解释中是这样说的："><span class="toc-text">yml配置文件在官方解释中是这样说的：</span></a></li><li class="toc-item toc-level-6"><a class="toc-link" href="#因此我这里在写hello-world时就直接按照上面说的来了。word在模板里面先用占位符，这样就可以-hello，小明-了o-╯□╰-o"><span class="toc-text">因此我这里在写hello world时就直接按照上面说的来了。word在模板里面先用占位符，这样就可以[hello，小明]了o(╯□╰)o</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#五、创建基础Configuration，用于进行反序列化。"><span class="toc-text">五、创建基础Configuration，用于进行反序列化。</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#六、进行创建基础Application"><span class="toc-text">六、进行创建基础Application</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#七、创建一个基础表示类，其需要使其转成JSON时遵循RFC-1149"><span class="toc-text">七、创建一个基础表示类，其需要使其转成JSON时遵循RFC 1149</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#八、创建Resource-类"><span class="toc-text">八、创建Resource 类</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#九、修改第六步时创建的Application中的run方法。"><span class="toc-text">九、修改第六步时创建的Application中的run方法。</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#十、创建一个HealthCheck类"><span class="toc-text">十、创建一个HealthCheck类</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#十一、再次修改第六步时创建的Application中的run方法。"><span class="toc-text">十一、再次修改第六步时创建的Application中的run方法。</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#十一、创建一个main方法。"><span class="toc-text">十一、创建一个main方法。</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#十二、修改maven的pom-xml文件"><span class="toc-text">十二、修改maven的pom.xml文件</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#十三、开始进行打包"><span class="toc-text">十三、开始进行打包</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#十四、开始启动"><span class="toc-text">十四、开始启动</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#看到上面的日志，则表明启动成功，打开浏览器访问："><span class="toc-text">看到上面的日志，则表明启动成功，打开浏览器访问：</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#至此，本次的Hello-World编写完成。"><span class="toc-text">至此，本次的Hello World编写完成。</span></a>
        
    </li></ol></li></div>
</aside>

                
            </div>
        </div>
    </section>
    <footer class="main-footer">
    <div class="container">
        <div class="row">
        </div>
    </div>
</footer>

<a id="back-to-top" class="icon-btn hide">
	<i class="fa fa-chevron-up"></i>
</a>




    <div class="copyright">
    <div class="container">
        <div class="row">
            <div class="col-sm-12">
                <div class="busuanzi">
    
</div>

            </div>
            <div class="col-sm-12">
                <span>Copyright &copy; 2017
                </span> |
                <span>
                    Powered by <a href="//hexo.io" class="copyright-links" target="_blank" rel="nofollow">Hexo</a>
                </span> |
                <span>
                    Theme by <a href="//github.com/shenliyang/hexo-theme-snippet.git" class="copyright-links" target="_blank" rel="nofollow">Snippet</a>
                </span>
            </div>
        </div>
    </div>
</div>







<script src="/js/app.js?rev=@@hash"></script>

</body>
</html>